package com.todod.base.module.base.controller.admin.apieisc;

import jakarta.annotation.security.PermitAll;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.todod.base.framework.common.pojo.PageParam;
import com.todod.base.framework.common.pojo.PageResult;
import com.todod.base.framework.common.pojo.CommonResult;
import com.todod.base.framework.common.util.object.BeanUtils;
import static com.todod.base.framework.common.pojo.CommonResult.success;

import com.todod.base.framework.excel.core.util.ExcelUtils;

import com.todod.base.framework.apilog.core.annotation.ApiAccessLog;
import static com.todod.base.framework.apilog.core.enums.OperateTypeEnum.*;

import com.todod.base.module.base.controller.admin.apieisc.vo.*;
import com.todod.base.module.base.dal.dataobject.apieisc.ApiEiscDO;
import com.todod.base.module.base.service.apieisc.ApiEiscService;

@Tag(name = "管理后台 - EISC系统接口")
@RestController
@RequestMapping("/base/api-eisc")
@Validated
public class ApiEiscController {

    @Resource
    private ApiEiscService apiEiscService;

    @PostMapping("/create")
    @Operation(summary = "创建EISC系统接口")
    public CommonResult<Long> createApiEisc(@Valid @RequestBody ApiEiscSaveReqVO createReqVO) {
        return success(apiEiscService.createApiEisc(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新EISC系统接口")
    public CommonResult<Boolean> updateApiEisc(@Valid @RequestBody ApiEiscSaveReqVO updateReqVO) {
        apiEiscService.updateApiEisc(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除EISC系统接口")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteApiEisc(@RequestParam("id") Long id) {
        apiEiscService.deleteApiEisc(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得EISC系统接口")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<ApiEiscRespVO> getApiEisc(@RequestParam("id") Long id) {
        ApiEiscDO apiEisc = apiEiscService.getApiEisc(id);
        return success(BeanUtils.toBean(apiEisc, ApiEiscRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得EISC系统接口分页")
    public CommonResult<PageResult<ApiEiscRespVO>> getApiEiscPage(@Valid ApiEiscPageReqVO pageReqVO) {
        PageResult<ApiEiscDO> pageResult = apiEiscService.getApiEiscPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ApiEiscRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出EISC系统接口 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportApiEiscExcel(@Valid ApiEiscPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ApiEiscDO> list = apiEiscService.getApiEiscPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "EISC系统接口.xls", "数据", ApiEiscRespVO.class,
                        BeanUtils.toBean(list, ApiEiscRespVO.class));
    }

    @GetMapping("/getEiscAccessToken")
    @PermitAll
    @Operation(summary = "获取用户Token")
    public CommonResult<String> getEiscAccessToken() {
        String accessToken = apiEiscService.getEiscAccessToken();
        return success(accessToken);
    }

    @GetMapping("/getEiscWellList")
    @PermitAll
    @Operation(summary = "获取井基本信息列表")
    public CommonResult<PageResult<EiscDataRespVO>> getEiscWellList(@RequestParam("accessToken") String accessToken, @Valid @RequestBody EiscPageReqVO pageReqVO) {
        PageResult<EiscDataRespVO> pageResult = apiEiscService.getEiscWellList(accessToken, pageReqVO);
        return success(BeanUtils.toBean(pageResult, EiscDataRespVO.class));
    }

}